package com.jy.adscm.operation.mask.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mask.bean.KzMaterial;
import com.jy.adscm.operation.mask.bean.KzMaterialDetail;
import com.jy.adscm.operation.mask.bean.QueryKzMaterial;
import com.jy.adscm.operation.mask.service.KzMaterialService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-03-24
 */
@RestController
@RequestMapping("/adscm/kz/kzMaterial")
public class KzMaterialController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    KzMaterialService kzMaterialService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody KzMaterial kzMaterial, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        kzMaterial.setCreateTime(new Date());
        kzMaterial.setInStocker(token.getUser().getId().toString());
        boolean insert = kzMaterialService.insertKzMaterial(kzMaterial);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody KzMaterial kzMaterial, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        kzMaterial.setCreateTime(new Date());
        boolean insert = kzMaterialService.updateById(kzMaterial);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryKzMaterial queryKzMaterial, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<KzMaterial> page = kzMaterialService.select(queryKzMaterial);
        return new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectByCode")
    public SimpleCoreResponse selectByCode( String code, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        KzMaterialDetail  kzMaterialDetails= kzMaterialService.selectByCode(code);
        return new SimpleCoreResponse("200","查询成功",kzMaterialDetails);
    }

}

